/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import explorer.ExplorerUnited;
import explorer.FileUnited;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user1
 */
@WebServlet(name = "servletUNITED", urlPatterns = {"/servletUNITED"})
public class ServletUnited extends HttpServlet {

    public static final String DOCTYPE =
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
    
    public static String file = null;
    public static String s;
    public static File f;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet servletUNITED</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet servletUNITED at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }
    
    public static String headWithTitle(String title, String query) {
    return(DOCTYPE + "\n" +
           "<HTML>\n" +
           "<HEAD><TITLE>" + title +":" + query + "</TITLE></HEAD>\n");
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
     public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(3);
    //Enumeration paramNames = request.getParameterNames();
    ExplorerUnited.setDir(request.getParameterValues("dir"));
    ExplorerUnited.setExplore(Integer.parseInt((request.getParameterValues("explore"))[0]));
    ExplorerUnited.setSearch(Integer.parseInt((request.getParameterValues("metode"))[0]));
    ExplorerUnited.startExplore(request.getParameterValues("query"));
    String title = "Hufft (-_-') Search";
    out.println("<font face=arial>" + headWithTitle(title,request.getParameterValues("query")[0]) +
                "<BODY BGCOLOR=\"#E6E6FA\">\n" +
                "<H1 ALIGN=CENTER style=color:blue>" + title + "</H1>\n" +
                "<TABLE ALIGN=LEFT width=100%>" +
                "<TR><TD width=100px>");
    if (request.getParameterValues("metode")[0].equals("0")) {
        s = "Brute Force";
    } else if (request.getParameterValues("metode")[0].equals("1")) {
        s = "Knuth-Morris-Pratt";
    } else {
        s = "Bayer Moore";
    }
    out.println("<i>pencarian</TD><TD>: </TD> </i><TD width=*><b>" + request.getParameterValues("query")[0] + "</b></TD><TD width=150px align=right><font size=2>" + s + "</font></TD></TR>" +
                "<TR><TD width=100px>");
     if (request.getParameterValues("explore")[0].equals("0")) {
        s = "DFS";
    } else {
        s = "BFS";
    }
    out.println("<i>direktori</TD><TD>: </TD> </i><TD width=*><b>" + request.getParameterValues("dir")[0] + "</b></TD><TD width=150px align=right><font size=2>" + s + "</font></TD></TR>" +
                "<TR><TD width=100px colspan=2></TD><TD width=*><font size=2 color=gray><i>Hasil : " + ExplorerUnited.result.size() + " (" + df.format((double)((double)ExplorerUnited.time/(double)1000000000)) + " detik)</i></font>");
    out.println("</TD><TD width=150px align=right><font size=2>Title + Content</font></TD></TR>" +
                "</TABLE><p><a href=\"" + "http://localhost:8080/stimaUNITED" + "\"><i><< back</i></a></p><HR width=100%>" + "<TABLE ALIGN=LEFT width=100%>\n" +
                "<TR>\n" +
                "<TD width=\"150\">&nbsp;<TD width=*>&nbsp;");
    for (FileUnited fileUnited : ExplorerUnited.result) {
        if (fileUnited.startJudul.size() > 0) {
            fileUnited.sortingJudul();
            out.println("<TR><TD /><TD>" + "<a href=\"" +
                    "openFileUnited" + "?id=" + fileUnited.getFilePath() + "\">" + "<font size=\"2\"> [" + fileUnited.getExtension() + "] </font>" + fileUnited.getName().substring(0, fileUnited.startJudul.get(0)));
            for (int i = 0; i < fileUnited.startJudul.size(); i++) {
                if (i+1 > fileUnited.startJudul.size()) {
                    out.println("<b>" + fileUnited.getName().substring(fileUnited.startJudul.get(i), fileUnited.startJudul.get(i)+fileUnited.tokenJudul.get(i))
                        + "</b>" + fileUnited.getName().substring(fileUnited.startJudul.get(i)+fileUnited.tokenJudul.get(i),fileUnited.startJudul.get(i+1)));
                } else {
                   out.println("<b>" + fileUnited.getName().substring(fileUnited.startJudul.get(i), fileUnited.startJudul.get(i)+fileUnited.tokenJudul.get(i))
                        + "</b>" + fileUnited.getName().substring(fileUnited.startJudul.get(i)+fileUnited.tokenJudul.get(i),fileUnited.getName().length()));
                }
                //out.print(fileUnited.tokenJudul.get(i) + "  ");
            }
            out.println("</a>\n" + "</TR>");
            out.println("<TR><TD /><TD><font size=2 color=green face=arial>" + fileUnited.getFullPath() + "</font></TD>");
            file = request.getParameterValues("dir")[0] + "\\";
        } else {
            out.println("<TR><TD /><TD>" + "<a href=\"" +
                    "openFileUnited" + "?id=" + fileUnited.getFilePath() + "\">" +
                     "<font size=\"2\"> [" + fileUnited.getExtension() + "] </font>" + (s = fileUnited.getName()) + "</a>\n" + "</TR>");
            out.println("<TR><TD /><TD><font size=2 color=green face=arial>" + fileUnited.getFullPath() + "</font></TD>");
        }
        if (fileUnited.startChar.size() > 0) {
            //Collections.sort(fileUnited.startChar);
            fileUnited.sorting();
            int prev = fileUnited.startChar.get(0);
            boolean b = false;
            int s = 0;
            for (int i = 0; i < fileUnited.startChar.size(); i++) {
                if (i+1 < fileUnited.startChar.size() && fileUnited.startChar.get(i+1) - prev <= 50 && b == false) {
                    out.println("<TR><TD /><TD>" + "..." +
                        fileUnited.getContent().substring(((fileUnited.startChar.get(i) - 25) >= 0 ? (fileUnited.startChar.get(i) - 25) : 0), fileUnited.startChar.get(i)) 
                        + "<b>" + fileUnited.getContent().substring(fileUnited.startChar.get(i),fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i)) + "</b>" 
                        + fileUnited.getContent().substring(fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i),fileUnited.startChar.get(i+1)));
                    s = i;
                    b = true;
                } else if (b) {
                    if (i+1 < fileUnited.startChar.size() && fileUnited.startChar.get(i+1) - prev <= 50) {
                        out.println("<b>" + fileUnited.getContent().substring(fileUnited.startChar.get(i),fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i)) + "</b>" 
                            + fileUnited.getContent().substring(fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i),fileUnited.startChar.get(i+1)));
                    } else {
                        out.println("<b>" + fileUnited.getContent().substring(fileUnited.startChar.get(i),fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i)) + "</b>" 
                        + fileUnited.getContent().substring(fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i),(fileUnited.startChar.get(i)+60 > fileUnited.getContent().length() ? fileUnited.getContent().length() : fileUnited.startChar.get(i)+60))+"..." + "</TD></TR>");
                        b = false;
                        if (i+1 < fileUnited.startChar.size())
                            prev = fileUnited.startChar.get(i+1);
                    }
                } else {
                    out.println("<TR><TD /><TD>" + "..." +
                        fileUnited.getContent().substring(((fileUnited.startChar.get(i) - 25) >= 0 ? (fileUnited.startChar.get(i) - 25) : 0), fileUnited.startChar.get(i)) 
                        + "<b>" + fileUnited.getContent().substring(fileUnited.startChar.get(i),fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i)) + "</b>" 
                        + fileUnited.getContent().substring(fileUnited.startChar.get(i)+fileUnited.tokenLength.get(i),(fileUnited.startChar.get(i)+60 > fileUnited.getContent().length() ? fileUnited.getContent().length() : fileUnited.startChar.get(i)+60)) 
                        +"..." + "</TD></TR>");
                    b = false;
                    if (i+1 < fileUnited.startChar.size())
                        prev = fileUnited.startChar.get(i+1);
                }
            }
        } else {
            out.println("<TR><TD>"+ "content search not found" + "</TD></TR>");
        }
        out.println("<TR><TD><p>&nbsp;</p></TD><TR>");
    }
    
    out.println("</TABLE>\n</font></BODY><font size=3 face=arial>@2011 13509052, 13509080, 13509088</font></HTML>");
  }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
      protected void doPost(HttpServletRequest request,
                         HttpServletResponse response)
          throws ServletException, IOException {
        doGet(request, response);
      }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
